package com.hundsun.yr.universal.p2p.bean;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.utils.HsDateUtils;
import com.hundsun.yr.universal.library.utils.HsNumberUtils;
import com.hundsun.yr.universal.library.utils.MathUtils;
import com.hundsun.yr.universal.library.utils.SpannableStringBuilderUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.p2p.config.Container;
import com.hundsun.yr.universal.p2p.utils.CommTool;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * Created by xx on 2017/4/18.
 */
public class ProductBeen implements Serializable {
    /**
     * app_flag : 1
     * ctime : 1492668894
     * debt : 10000.00
     * id : 8
     * invest_max : 10000.00
     * invest_max_sum : 10000.00
     * invest_min : 1000.00
     * invest_rate : 8.5000
     * invest_step : 1000.00
     * name : 车分融04201414
     * overdue : 0
     * pay_method : 4
     * prepayment_flag : 0
     * product_type : 4
     * progress : 10
     * raise_term : 2
     * raise_time : 2017-04-20 14:14:55
     * recruit_flag : 0
     * status : 1
     * term : 12
     * top_flag : 1
     * utime : 1492668929
     * uuid : 0001fb
     * web_flag : 1
     * debt_rate : 7.5000
     * discount_rate : 0.0000
     * guarantee_fee : 0.0000
     * int_pay_day : 01
     * loan_user_no : 20170405164949682962
     * pledge_fee : 0.0000
     * prepayment_begin : 0000-00-00
     * prepayment_end : 0000-00-00
     * prepayment_fee : 0.0000
     * purpose : 消费
     * raise_percent : 0
     * raise_time_wait : 0   0已经开售其他时候还在跳时间
     * repay_user_no : 20170405164949682962
     * risk_fee : 1.0000
     * service_fee : 1.0000
     * user_no : 20170405164949682962
     * end_time : 2017-06-24
     * start_time : 2017-04-24
     * true_debt : 200000.00
     * repay_time : 2017-04-22 23:20:15
     *
     */

    /**
     * 是否在App上展示
     **/
    public String app_flag;
    /**
     * 创建时间
     **/
    public String ctime;
    /**
     * 借款金额
     **/
    public String debt;
    /**
     * 主键产品唯一id
     **/
    public String id;
    /**
     * 单笔投资最大金额
     **/
    public String invest_max;
    /**
     * 累计投资最大金额
     **/
    public String invest_max_sum;
    /**
     * 单笔投资最小金额
     **/
    public String invest_min;
    /**
     * 投资利率
     **/
    public String invest_rate;
    /****/
    public String invest_step;
    /**
     * 商品名称
     **/
    public String name;
    /**
     * 逾期标志
     **/
    public String overdue;
    /**
     * 还款方式 1、到期还本付息 2、先息后本 3、等额本息
     **/
    public String pay_method;
    /**
     * 是否允许提前还款
     **/
    public String prepayment_flag;
    /**
     * 产品分类ID
     **/
    public String product_type;
    /**
     * 商品处理状态
     **/
    public String progress;
    /**
     * 最长募集天数
     **/
    public String raise_term;
    /**
     * 开始募集时间
     **/
    public String raise_time;
    /**
     * 新手标标签
     **/
    public String recruit_flag;
    /**
     * 状态
     **/
    public String status;
    /**
     * 借款期限
     **/
    public String term;
    /**
     * 首页置顶标签
     **/
    public String top_flag;
    /**
     * vip标签
     **/
    public String vip_flag;
    /**
     * utime
     **/
    public String utime;
    /**
     * 产品的UUID(即信的流水号）
     **/
    public String uuid;
    /**
     * 是否在网站上展示
     **/
    public String web_flag;
    /**
     * 借款利率
     **/
    public String debt_rate;
    /**
     * 贴现利率
     **/
    public String discount_rate;
    /**
     * 服务利率
     **/
    public String guarantee_fee;
    /**
     * 按月付息模式的还款日期
     **/
    public String int_pay_day;
    /**
     * 放款人User No
     **/
    public String loan_user_no;
    /**
     * 抵押费率(%)
     **/
    public String pledge_fee;
    /**
     * 开始允许提前还款的日期
     **/
    public String prepayment_begin;
    /**
     * 停止提前还款的日期（之后的还款，需全额还款）
     **/
    public String prepayment_end;
    /****/
    public String prepayment_fee;
    /**
     * 借款用途
     **/
    public String purpose;
    /****/
    public int raise_percent;
    /**
     * 是否可投资
     **/
    public int raise_time_wait;
    /**
     * 还款人User No
     **/
    public String repay_user_no;
    /**
     * 风险准备金
     **/
    public String risk_fee;
    /**
     * 服务费率
     **/
    public String service_fee;
    /**
     * 借款人User No
     **/
    public String user_no;
    /**
     * 预计商品结束时间
     **/
    public String end_time;
    /**
     * 起息时间
     **/
    public String start_time;
    /**
     * 实际借款
     **/
    public String true_debt;
    /**
     * 实际还款时间
     **/
    public String repay_time;
    /**
     * 已募集金额
     **/
    public String raise_amt;
    /**
     * 已募集金额2
     **/
    public String raise_amount;
    /**
     * raise_progress : 0
     */

    public int raise_progress;

    public String use_discount_flag;

    public String basic_debt_rate;

    /**
     * 项目星级
     */
    public int star_level;

    /**
     * 项目星级描述
     */
    public String star_level_desc;

    /**
     * 风险等级
     */
    public String risk_level;

    /**
     * 风险等级描述
     */
    public String risk_msg;

    /**
     * 产品募集状态
     */
    public int product_status;
    /**
     * apply_id : null
     * true_debt : null
     * raise_amt : null
     * guarantee_flag : null
     * pledge_flag : null
     * vip_flag : null
     * invite_user_phone : null
     * start_time : null
     * end_time : null
     * repay_time : null
     * add_debt_rate : 3.0000
     * short_desc : 已加息
     * raise_amount : 0
     */

    public String add_debt_rate;
    public String short_desc;

    public String favor_flag;
    public String add_rate_msg;
    @Override
    public String toString() {
        return "ProductBeen{" +
                "app_flag='" + app_flag + '\'' +
                ", ctime='" + ctime + '\'' +
                ", debt='" + debt + '\'' +
                ", id='" + id + '\'' +
                ", invest_max='" + invest_max + '\'' +
                ", invest_max_sum='" + invest_max_sum + '\'' +
                ", invest_min='" + invest_min + '\'' +
                ", invest_rate='" + invest_rate + '\'' +
                ", invest_step='" + invest_step + '\'' +
                ", name='" + name + '\'' +
                ", overdue='" + overdue + '\'' +
                ", pay_method='" + pay_method + '\'' +
                ", prepayment_flag='" + prepayment_flag + '\'' +
                ", product_type='" + product_type + '\'' +
                ", progress='" + progress + '\'' +
                ", raise_term='" + raise_term + '\'' +
                ", raise_time='" + raise_time + '\'' +
                ", recruit_flag='" + recruit_flag + '\'' +
                ", status='" + status + '\'' +
                ", term='" + term + '\'' +
                ", top_flag='" + top_flag + '\'' +
                ", utime='" + utime + '\'' +
                ", uuid='" + uuid + '\'' +
                ", web_flag='" + web_flag + '\'' +
                ", debt_rate='" + debt_rate + '\'' +
                ", discount_rate='" + discount_rate + '\'' +
                ", guarantee_fee='" + guarantee_fee + '\'' +
                ", int_pay_day='" + int_pay_day + '\'' +
                ", loan_user_no='" + loan_user_no + '\'' +
                ", pledge_fee='" + pledge_fee + '\'' +
                ", prepayment_begin='" + prepayment_begin + '\'' +
                ", prepayment_end='" + prepayment_end + '\'' +
                ", prepayment_fee='" + prepayment_fee + '\'' +
                ", purpose='" + purpose + '\'' +
                ", raise_percent=" + raise_percent +
                ", raise_time_wait=" + raise_time_wait +
                ", repay_user_no='" + repay_user_no + '\'' +
                ", risk_fee='" + risk_fee + '\'' +
                ", service_fee='" + service_fee + '\'' +
                ", user_no='" + user_no + '\'' +
                ", end_time='" + end_time + '\'' +
                ", start_time='" + start_time + '\'' +
                ", true_debt='" + true_debt + '\'' +
                ", repay_time='" + repay_time + '\'' +
                ", raise_amt='" + raise_amt + '\'' +
                ", raise_amount='" + raise_amount + '\'' +
                '}';
    }

    /**
     * 获取投资期限单位
     *
     * @param pay_method
     * @return
     */
    public static String getPayMethod(String pay_method) {

        String limttype = "";

        switch (pay_method) {
            case "1":
                limttype = "天";
                break;
            default:
                limttype = "月";
                break;
        }
        return limttype;
    }


    /**
     * 显示投资期限
     *
     * @return
     */
    public String getTerm() {
        String duration = HsNumberUtils.numberFormat("##0", term) + getPayMethod(pay_method);
        return duration;
    }

    public String getTrem (){
        return term + getPayMethod(pay_method);
    }

    /**
     * 显示投资百分比
     */
    public String getPercent() {
        if (debt != null && raise_amount != null) {
            return HsNumberUtils.formartPercent(MathUtils.subtract(debt, Double.valueOf(debt) - Double.valueOf(raise_amount) + ""), debt);
        } else {
            return "0";
        }
    }

    /**
     * 显示进度
     */
    public int dispalyProgress() {
        int progress = MathUtils.string2int(getPercent());
        return progress;
    }

    public String displayChangeAmount() {
        DecimalFormat df = new DecimalFormat("######0.00");
        try{
            return  df.format(Double.valueOf(debt) - Double.valueOf(raise_amount));
        } catch (Exception e){
            return "0.00";
        }
    }

    public String getDebet() {
        DecimalFormat df = new DecimalFormat("######0.00");
        try{
            return  df.format(Double.valueOf(debt));
        } catch (Exception e){
            return "0.00";
        }
    }

    /**
     * 设置默认值
     */
    public void setDefaultMark() {
        debt = "0";
        raise_amount = "0";
        invest_rate = "0";
        add_debt_rate = "0";
        basic_debt_rate = "0";
        pay_method = "1";
        term = "0";
        invest_min = "0";
        name = "敬请期待";
        uuid = "0000";
    }

    /**
     * 进度代码	   含义	    对应恒生系统代码
     * 0	          未发布    0：未发布
     * 10			  投标中	1：投标中
     * 15			  待放款	无
     * 18			  放款中	无
     * 20			  计息中	2：计息中
     * 30			  待返还（暂时不用）	3：待返还
     * 40			  已返还	4：已返还
     * 50			  已结束	5：已结束
     * 60			  已撤销	6：已结束
     * 100		  逾期未返还
     * 105		  逾期部分返还（暂不开发）
     * 110		  逾期已返还
     * 120		  流标
     * 125		  停售（募集期截止但未募满）
     * 130		  风控拒绝放款
     * 200		  逾期已返还(与11重复废弃)
     */
    public void refreshStatus(Context context, HSButton btn_buy, ProgressBar invest_progress , int barProgress,TextView tv_surplus_invest,int product_status) {
        switch (product_status){
            case 0://未开售
                //未开售
                try {
                    String stringTime = Container.getInstance().getStringTime();
                    String date = raise_time;
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
                    //      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");//12小时制
                    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                    long time = simpleDateFormat.parse(date).getTime();
                    Container.getInstance().setServerTime(time);
                    String crnStartTime = MathUtils.subtract(time + "", stringTime);
                    if (MathUtils.compare(crnStartTime, "0") > 0) {
                        btn_buy.setEnabled(false);
                        btn_buy.setText(context.getString(R.string._project_starttime, HsDateUtils.formatTimeHourEN(MathUtils.string2long(crnStartTime))));
                        /**
                         * 投资占比，不可投
                         */
                        invest_progress.setSecondaryProgress(barProgress);
                        tv_surplus_invest.setText(context.getString(R.string.surplus_vote_, displayChangeAmount()));
                    } else {
                        btn_buy.setEnabled(true);
                        btn_buy.setText(SpannableStringBuilderUtils.sizeSpan(context.getString(R.string.invest_and_surplus,CommTool.keep2Digits(invest_min)),context.getString(R.string.buy_now),16));
                        invest_progress.setProgress(barProgress);
                        tv_surplus_invest.setText(context.getString(R.string.surplus_vote_, displayChangeAmount()));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 1://募集中
                btn_buy.setEnabled(true);
                btn_buy.setText(SpannableStringBuilderUtils.sizeSpan(context.getString(R.string.invest_and_surplus,CommTool.keep2Digits(invest_min)),context.getString(R.string.buy_now),16));
                invest_progress.setProgress(barProgress);
                tv_surplus_invest.setText(context.getString(R.string.surplus_vote_, displayChangeAmount()));
                break;
            case 2://满标
                btn_buy.setEnabled(false);
                btn_buy.setText(R.string.sale_out_zwgt);
                invest_progress.setProgress(barProgress);
                tv_surplus_invest.setText(context.getString(R.string.surplus_vote_, displayChangeAmount()));
                break;
            case 3://停售
                btn_buy.setEnabled(false);
                btn_buy.setText(R.string.sale_finish);
                invest_progress.setProgress(barProgress);
                tv_surplus_invest.setText(context.getString(R.string.surplus_vote_, displayChangeAmount()));
                break;
            case 4://还款中
                btn_buy.setEnabled(false);
                btn_buy.setText(R.string.progress40);
                invest_progress.setProgress(barProgress);
                tv_surplus_invest.setText(context.getString(R.string.surplus_vote_, displayChangeAmount()));
                break;
            case 5://已还款
                btn_buy.setEnabled(false);
                btn_buy.setText(R.string.payment_progress1);
                invest_progress.setProgress(barProgress);
                tv_surplus_invest.setText(context.getString(R.string.surplus_vote_, displayChangeAmount()));
                break;
            case 6://计息中
                btn_buy.setEnabled(false);
                btn_buy.setText(R.string.mine_transfering);
                invest_progress.setProgress(barProgress);
                tv_surplus_invest.setText(context.getString(R.string.surplus_vote_, displayChangeAmount()));
                break;
        }
    }

    public String getPayMethod(Context mContext) {
        String payMethod = "";
        switch (pay_method) {
            case "1":
                payMethod = mContext.getString(R.string.debt_service_at_maturity);
                break;
            case "2":
                payMethod = mContext.getString(R.string.first_coupon_later_edition);
                break;
            case "3":
                payMethod = mContext.getString(R.string.monthly_equal_principal_and_interest);
                break;
            case "4":
                payMethod = mContext.getString(R.string.monthly_principal);
                break;
        }
        return payMethod;
    }
}
